package cn.b.sky.mes.qm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.qm.dao.DefectAttributeEnumDao;
import cn.b.sky.mes.qm.model.DefectAttribute;
import cn.b.sky.mes.qm.model.DefectAttributeEnum;
import cn.b.sky.mes.qm.model.DefectAttributeValue;
import cn.b.sky.mes.qm.model.condition.DefectAttributeCondition;
import cn.b.sky.mes.qm.service.DefectAttributeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.mes.qm.dao.DefectAttributeDao;

@Service("defectAttributeService")
public class DefectAttributeServiceImpl implements DefectAttributeService {
  
  private static final Logger LOG = LoggerFactory.getLogger(DefectAttributeServiceImpl.class);

  @Resource
  DefectAttributeDao defectAttributeDao;

  @Resource
  DefectAttributeEnumDao defectAttributeEnumDao;

  @Override
  public List<DefectAttribute> selectDefectAttributeListByCondition(DefectAttributeCondition condition) {
    if (condition == null) {
      condition = new DefectAttributeCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return defectAttributeDao.selectDefectAttributeListByCondition(condition);
  }

  @Override
  public int selectDefectAttributeCountByCondition(DefectAttributeCondition condition) {
    if (condition == null) {
      condition = new DefectAttributeCondition();
    }
    return defectAttributeDao.selectDefectAttributeCountByCondition(condition);
  }

  @Override
  public DefectAttribute getDefectAttribute(String siteCode, String defectAttributeCode) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(defectAttributeCode)) {
      return null;
    }
    return defectAttributeDao.getDefectAttribute(siteCode, defectAttributeCode);
  }

  @Override
  public void insertDefectAttribute(DefectAttribute defectAttribute) {
    if (StringUtils.isBlank(defectAttribute.getSiteCode()) || StringUtils.isBlank(defectAttribute.getAttributeCode())) {
      return;
    }
    List<DefectAttributeEnum> attributeEnums = defectAttribute.getAttributeEnum();
    defectAttributeDao.insertDefectAttribute(defectAttribute);
    for (DefectAttributeEnum attributeEnum : attributeEnums) {
      defectAttributeEnumDao.insertDefectAttributeEnum(attributeEnum);
    }
  }

  @Override
  public void updateDefectAttribute(DefectAttribute defectAttribute) {
    if (StringUtils.isBlank(defectAttribute.getSiteCode()) || StringUtils.isBlank(defectAttribute.getAttributeCode())) {
      return;
    }
    List<DefectAttributeEnum> attributeEnums = defectAttribute.getAttributeEnum();
    defectAttributeDao.updateDefectAttribute(defectAttribute);
    defectAttributeEnumDao.deleteDefectAttributeEnum(defectAttribute.getAttributeCode(), defectAttribute.getSiteCode());
    for (DefectAttributeEnum attributeEnum : attributeEnums) {
      defectAttributeEnumDao.insertDefectAttributeEnum(attributeEnum);
    }

  }

  @Override
  public BatchJobReport deleteDefectAttribute(String siteCode, String defectAttributeCode) {
    String languageCode = SecurityContextUtil.getLanguageCode();
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(defectAttributeCode)) {
      return new BatchJobReport("", "siteCode or defectAttributeCode is null", false, languageCode, SecurityContextUtil.getSiteCode());
    }
    String message = defectAttributeCode;
    boolean success = true;
    String cause = "";

    try {
      defectAttributeDao.deleteDefectAttribute(siteCode, defectAttributeCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(errorMsg)) {
        //外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success, languageCode, SecurityContextUtil.getSiteCode());
  }

  @Override
  public List<DefectAttributeEnum> selectDefectAttributeEnumList(String siteCode, String defectAttributeCode) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(defectAttributeCode)) {
      return null;
    }
    return defectAttributeEnumDao.selectDefectAttributeEnumList(defectAttributeCode, siteCode);
  }

  @Override
  public List<DefectAttribute> selectDefectAttributeAndEnumByLockFlag(String siteCode, Integer lockFlag) {
    if (StringUtils.isBlank(siteCode)) {
      return null;
    }
    return defectAttributeDao.selectDefectAttributeAndEnumByLockFlag(siteCode, lockFlag);
  }

  @Override
  public List<DefectAttributeValue> getDefectAttributeValuesForGraph(String defectCode, String siteCode) {
    if (StringUtils.isEmpty(siteCode) || StringUtils.isEmpty(defectCode)) {
      return null;
    }
    return defectAttributeDao.getDefectAttributeValuesForGraph(defectCode, siteCode);
  }

  @Override
  public List<DefectAttributeValue> getAllDefectAttributeAndValue(String id, String siteCode) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(id)) {
      return null;
    }
    return defectAttributeDao.getAllDefectAttributeAndValue(id, siteCode);
  }

}
